#|
   L.I.S.P.: Lots of irritating and silly parentheses.

   Henrique "Nican" Polido.
   Keleigh O'Neil

|#

;; Starter file for CS1102 lab 6 on state and web programming
;; Charles Rich (adapted from Kathi Fisler), WPI
(require racket/list)

(load "server.rkt") ;file must be in same folder

;; Note: Much advisable error checking is omitted here in order
;; not to clutter the code, so that the main pedagogical points
;; are more evident -CR

;;;;;;; DATA DEFINITIONS ;;;;;;;;;;;;;;;;;;;;;;;;

;; a post is (make-post string string)
(define-struct post (name body))

;;POSTS is the global variable that keeps track of all the posts.
(define POSTS (list (make-post "Nican" "It is so easy being evil!")))

;;get-posts: void -> list[list[symbol]...]
;;returns a list of all the posts onto the main page
(define (get-posts)
  (map (lambda (post)
         `(table ([border "1"][width "100%"])
                 (tr 
                  (td
                   ,(post-name post)))
                 (tr 
                  (td
                   ,(post-body post)))))
       POSTS))

;; main: form cookies -> html-page cookies
;; returns list of posts and a post button
(define-script (main form cookies) ;args ignored
  (values 
   (html-page "Posts"
              "Welcome to the forums!"
              (append 
               '(div)
               (get-posts)
               )
              `(a ([href "post"]) "Post")
              )false))

;; post: form cookies -> html-page cookies
;; returns a form with fields to add comment with name and body 
(define-script (post form cookies)
  (let ([head (cdr (or (assoc 'name form) '(nothing . "Enter your name here")))]
        [text (cdr (or (assoc 'body form) '(nothing . "Enter your text here")))])
    (values 
     (html-page "Write"
                `(form ([action "preview"][target "_blank"]) ;in new window/tab... WHY?!?!?!
                       (p)
                       (input ([type "text"] [name "name"] [value ,head]))
                       (p)
                       (textarea ([name "body"]) ,text)
                       (p)
                       (input ([type "submit"] [value "Preview"]))
                       )
                ;; no cookie
                )false)))

;; preview: form cookies -> html-page cookies
;; return a table with proposed comment and submit button & go back button
(define-script (preview form cookies)
  (let [(button (lambda(name target)
                  `(form ([action ,target][target "_self"])
                     (input ([type "hidden"][name "name"][value ,(cdr (assoc 'name form))]))
                     (input ([type "hidden"][name "body"][value ,(cdr (assoc 'body form))]))
                     (input ([type "submit"] [value ,name]))
                     )))]
  (values 
   (html-page "Preview"
              `(table ([border "1"])
                      (tr
                       (td 
                        ,(cdr (assoc 'name form))))
                      (tr
                       (td 
                        ,(cdr (assoc 'body form)))))
              (button "Go Back!" "post")
              '(p)
              (button "Post!" "makepost"))
   ;; no cookie
   false)))

;; makepost form cookies -> html-page cookies
;; submits the post and redirects the user to the main page 
(define-script (makepost form cookies)
  (set! POSTS 
        (cons 
         (make-post 
          (cdr (assoc 'name form))
          (cdr (assoc 'body form))) POSTS))
  (values 
   (list 'html
         (list 'head (list 'title "Title")
               '(meta ([http-equiv "Refresh"] [content "0;url=main"])))
         
         '(body 
           "Redirecting..."))
   ;; no cookie
   false))



(server 8088)